<?php
error_reporting(E_ALL);
require_once 'common.inc.php';
$time = time();
$time24 = $time - (60 * 60 * 24);
$timed = date('Y-m-d H:i:s', $time);
$time24d = date('Y-m-d H:i:s', $time24);
$tracker = new dbstuff;
$tracker -> connect($dsdbhost, $dsdbuser, $dsdbpw, $traname, $dspconnect);
$tra_db = $tracker;

$count = new dbstuff;
$count -> connect($dphost, $dpuser, $dppw, $db_torrent, $dppconnect);

$file = "./deploy/alarm_state.php";
$change = $time - filemtime($file);

if ($change >= 3) {
	include './deploy/alarm_state.php';
	include './deploy/alarm_config.php';
	if ($type == 'acs') {
		// table area
		$sql = "select * from area order by `describe` asc";
		$query = $dsdb -> query($sql);

		while ($result = $dsdb -> fetch_array($query)) {
			$freespace[] = $result['freespace'];
			if (!in_array($result['describe'], $listip)) {
				$oldip = $result['describe'];
				$listip[] = $oldip;
			} 
		} 
		// total flow?
		// table acsconnect
		$sql = "select count(*)num from acsconnect where atime<='$time' and atime>='$time24'";
		$query = $tra_db -> query($sql);
		$result = $tra_db -> fetch_array($query);
		$linknum = $result['num']; 
		// $oneflow = ceil($totalflow/$linknum);

		$sql = "select count(*)seednum,B.`describe`,B.port from localhash_1 A left join area B on A.btcid=B.btcid where A.uptime<='$timed' and A.uptime>='$time24d' and A.btcid<>0 group by A.btcid order by B.`describe`";
		$query = $dsdb -> query($sql);
		while($result = $dsdb -> fetch_array($query)){
			$seednum[] =$result['seednum'];
		}		
		
		// seed prep?
		$imgflow_acs = compare($flow, $acs[0]);
		$imgoneflow_acs = compare($oneflow, $acs[1]);
		$imgseednum_acs = compare($seednum, $acs[2]);
		$imgseedprep_acs = compare($seedprep, $acs[3]);
		$imgspace_acs = compare($freespace, $acs[4]);
	} elseif ($type == 'rds') {
		$sql = "select sum(ccount),host from c_torrent where ctime <= '$time' and ctime>='$time24' group by host order by host asc";
		$query = $count -> query($sql);
		while($result = $count -> fetch_array($query)){
			$seedsum[]=$result['sum(ccount)'];
		}

		$sql = "select sum(usercount) from c_ues where ctime<='$time' and ctime>='$time24'";
		$query = $count -> query($sql);
		$usesum = $count -> fetch_array($query);
		// GET
		$imgseednum_rds = compare($seedsum, $rds[0]);
		$imgusesum_rds = compare($usesum, $rds[1]);

	} elseif ($type == 'wacs') {
	} elseif ($type == 'woso') {
		/*$sql = "select count(*)visit from woso_count where time<='$time' and time>='$time24'";
		$query = $count -> query($sql);
		$visitnum = $count -> fetch_array($query);*/

		$sql = "select count(*) from ps_count where ctime<='$time' and ctime>='$time24'";
		$query = $dp -> query($sql);
		$shownum = $dp -> fetch_array($query);

		$sql = "select count(*)down from ps_down where dtime<='$time' and dtime>='$time24'";
		$query = $dp -> query($sql);
		$downnum = $dp -> fetch_array($query);

		//$imgvisitnum_woso = compare($visitnum, $woso[0]);
		$imgshownum_woso = compare($shownum, $woso[1]);		
		$imgdownnum_woso = compare($downnum, $woso[2]);
	} elseif ($type == 'tracker') {
		$sql = "select sum(tracount) from c_ues where ctime<='$time' and ctime>='$time24'";
		$query = $count -> query($sql);
		$trasum = $count -> fetch_array($query);

		$imgtrasum_tra=compare($trasum,$tra[0]);
	}
	$str = "<?php \n".
		"\$imgflow_acs = array('".implode('\',\'',$imgflow_acs)."');\n".
		"\$imgoneflow_acs = array('".implode('\',\'',$imgoneflow_acs)."');\n".
		"\$imgseednum_acs = array('".implode('\',\'',$imgseednum_acs)."');\n".
		"\$imgseedprep_acs = array('".implode('\',\'',$imgseedprep_acs)."');\n".
		"\$imgspace_acs = array('".implode('\',\'',$imgspace_acs)."');\n".
		"\$imgseednum_rds = array('".implode('\',\'',$imgseednum_rds)."');\n".
		"\$imgusesum_rds = array('".implode('\',\'',$imgusesum_rds)."');\n".
		"\$imgvisitnum_woso = array('".implode('\',\'',$imgvisitnum_woso)."');\n".
		"\$imgshownum_woso = array('".implode('\',\'',$imgshownum_woso)."');\n".
		"\$imgdownnum_woso = array('".implode('\',\'',$imgdownnum_woso)."');\n".
		"\$imgtrasum_tra = array('".implode('\',\'',$imgtrasum_tra)."');\n".
		"?>";
	write_file($file,$str);
} else {

} 

function readover($filename){
	$filedata=file_get_contents($filename);
	return $filedata;
}

function write_file($filename, $str) {
	$fp = fopen($filename, 'wb+');
	if ($fp) {
		fwrite($fp, $str);
		fclose($fp);
	} 
} 
function compare($list, $standard) {
	foreach($list as $key => $value) {
		if ($value >= $standard) {
			$img[$key] = '<img src="images/normal.jpg" />';
		} elseif ($value < $standard) {
			$img[$key] = '<img src="images/breakdown.jpg" />';
		} 
	} 
	return $img;
} 

?>